// 客户属性值转换
export const getValueText = (value, mapping) =>{  
  const item = mapping.find(item => item.value === value);
  return item ? item.text : '';
}

// 时间戳转换
export const timestampToDate = (e) => {
	const date = e.length < 13 ? new Date(e * 1000) : new Date(e)
	const Year = date.getFullYear();
	const Moth = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
	const Day = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate());
	const Hour = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours());
	const Minute = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes());
	const Sechond = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
	return  Year + '-' + Moth + '-' + Day + '   '+ Hour +':'+ Minute  + ':' + Sechond;
}

// 时间戳转换
export const formatTimestamp = (timestamp) => {  
    const now = Date.now();  
    const diffInSeconds = Math.round((now - timestamp) / 1000);  
    const diffInMinutes = Math.floor(diffInSeconds / 60);  
    const diffInHours = Math.floor(diffInSeconds / (60 * 60));  
    const diffInDays = Math.floor(diffInSeconds / (60 * 60 * 24));  
  
    if (diffInMinutes < 10) {  
        return '刚刚';  
    } else if (diffInMinutes < 60) {  
        return `${Math.floor(diffInMinutes / 10) * 10}分钟内`; // 近似到最近的10分钟  
    } else if (diffInHours < 24) {  
        return `${diffInHours}小时内`;  
    } else if (diffInDays < 2) {  
        return '1天内';  
    } else if (diffInDays < 8) {  
        return `${diffInDays}天内`;  
    } else if (diffInDays < 31) {  
        return '30天内';  
    } else {  
        const diffInMonths = Math.floor(diffInSeconds / (60 * 60 * 24 * 30));  
        if (diffInMonths < 6) {  
            return `${diffInMonths}月内`;  
        } else if (diffInMonths < 12) {  
            return '半年内';  
        } else {  
            const diffInYears = Math.floor(diffInSeconds / (60 * 60 * 24 * 365));  
            if (diffInYears < 2) {  
                return '一年内';  
            } else {  
                return '超过一年';  
            }  
        }  
    }  
}

// 城市转换
export const findCityAndDistrict = (value, cityArray) =>{  
    for (let i = 0; i < cityArray.length; i++) {  
        const city = cityArray[i];  
        const childMatch = city.children.find(child => child.value === value);  
        if (childMatch) {  
            return `${city.text}/${childMatch.text}`;  
        }  
    }
    return null;  
}

// 资质转换
export const getTextsFromValues = (valueArray, certifications)  => {
	return valueArray.map(value => {  
		const certification = certifications.find(cert => cert.value === value);
		return certification ? certification.text : '未知';  
	}).join('、')
}

// 调整数组顺序
export const adjustArrayOrder = (arr)=>{
	const nameIndex = arr.indexOf('name');  
	if (nameIndex !== -1) { 
	    if (nameIndex > 1) {   
	        [arr[1], arr[nameIndex]] = [arr[nameIndex], arr[1]];  
	    } else if (nameIndex < 1) {  
	       [arr[0], arr[1]] = [arr[1], arr[0]]  
	    }
	}
	return arr
}

// 对象提取
export const getNonStringProperties = (obj)=>{
	let certification = []
	let result = {};
	let newObj = JSON.parse(JSON.stringify(obj))
	
	if(newObj.label !==''){
		certification = [newObj.label]
	}
	if(newObj.house !== '' && !certification.includes(newObj.house)){
		certification = [...certification, newObj.house]
	}
	if(newObj.car !== '' && !certification.includes(newObj.car)){
		certification = [...certification, newObj.car]
	}
	
	if(certification.length>0)	newObj.certification = certification
	
	
	delete newObj.house;
	delete newObj.car;
	delete newObj.label;
    for (let key in newObj) { 
        if ( newObj[key] !== '' || (typeof newObj[key] === 'string' && newObj[key].trim() !== '')) {  
			
            result[key] = typeof newObj[key] === 'string' ? newObj[key].trim() : newObj[key];  
        }  
    }
	console.log(result);
    return result;  
}  
  
  